const express=require('express');
const router=express.Router();
const svgCaptcha=require("svg-captcha")
const db=require('../model/user')
router.post("/doRegister",async (req,res)=>{
  console.log(req.body)
  var data={status:0,msg:null}
  var existed=await db.User.find({account:req.body.account})
  console.log(existed)
  if (!existed.length&&req.body.captcha.toLocaleLowerCase()==req.session.captcha.toLocaleLowerCase()&&req.body.account.length<15&&req.body.nickName.length<20&&req.body.password) {
    console.log('注册成功')
    data.status=1
    data.msg="success"
    var userInfo=new db.User({
      nickName:req.body.nickName,
      account:req.body.account,
      password:req.body.password
    })
    userInfo.save();
    req.session.nickName=req.body.nickName
    res.status(200).send(data)
  }else{
    data.status=0
    data.msg='注册失败'
    if(existed)
    data.status=2
    res.status(200).send(data)
  }

})
router.get('/captcha',(req,res)=>{
  var captcha = svgCaptcha.create();
  console.log(captcha.text);
  req.session.captcha = captcha.text;
  res.type("svg");
  res.status(200).send(captcha.data);
  
})

module.exports=router
